﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Data
{
    class Function
    {
       
        SqlConnection con = new SqlConnection();
        public Function(string ip, string database)
        {
            con = connection.Connect(ip, database);
        }
        DataTable tbl;
        string sql;
        SqlCommand cmd;
        public DataTable LayDuLieu(string tenBang, string cot, string dieukien)
        {
            if (dieukien != "")
                sql = "select " + cot + " from " + tenBang + " " + dieukien;
            else
                sql = "select " + cot + " from " + tenBang;
            cmd = new SqlCommand(sql, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(tbl);
            return tbl;
            //minh chi dua vao cau len cap nhat thoi nhe roi ban lam tiep di
            //moi nguoi y cung hoi kho hihi hihi gio minh moi co quy uoc chung va giai thich ne vi mai minh con dung nhung gi cua nhau nua ma
            //roi ban lam tiep di
        }
        public void GhiDuLieu(string tenBang, string giatri)
        {
            sql = "Insert into " + tenBang + " values (" + giatri + ")";
            cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
            
        }
        public void XoaDuLieu(string tenbang,string giatri)
        {
            sql = "Delete from" + tenbang + "where " + giatri;
            cmd = new SqlCommand(sql,con);
            cmd.ExecuteNonQuery();
        }
        public void SuaDuLieu(string tenBang, string tencot,string dieukien)
        {
            sql = "Update " + tenBang + " set " + tencot + " where " + dieukien;
            cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();
        }
        public void DemSoDong(string tenbang,string dieukien)
        {
            sql = "Select count(*) from" + tenbang + "where" + dieukien;
            cmd = new SqlCommand(sql,con);
            cmd.ExecuteNonQuery();
        }
        public bool KiemTraHocVien(string tenbang,string mahv)
        {
            string sql = "select * from" +tenbang+" where" + mahv;
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 1)
                return true;
            else
                return false;
        }
        public void LuuDiem(string tenBang, string giatri)
        {
            sql = "Insert into " + tenBang + " values (" + giatri + ")";
            cmd = new SqlCommand(sql, con);
            cmd.ExecuteNonQuery();

        }
        
        public bool DoiMatKhau(string tenbang,string user, string pass)
        {
            sql = "Update " + tenbang + " set " + user + " where " + pass;
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return true;
        }
        public bool kiemtrauser(string tenbang,string user, string pass)
        {
            sql = "select * from " + tenbang + " where" + user + " AND " + pass;
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 1)
                return true;
            else
                return false;
        }
        


        
        
    }
}
